package com.chrray.position.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import io.swagger.annotations.ApiModel;
import lombok.Data;

import java.util.List;

/**
 * 用户资质证书 对象
 *
 * @author wusiwee
 * @email wusiwee@163.com
 * @since 1.0.1 2024-08-07
 */

@Data
@ApiModel(description = "用户资质证书")
@TableName("fd_user_position_rel")
public class FdUserPositionRel extends Model<FdUserPositionRel> {
	@TableId(type = IdType.AUTO)
	private Long id;
	private Long userId;
	private Long positionId;
	private Long intentionId;
	private String positionName;


	public static void deleteByUserId(Long userId) {
		FdUserPositionRel ent = new FdUserPositionRel();
		LambdaQueryWrapper<FdUserPositionRel> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(FdUserPositionRel::getUserId, userId);
		ent.delete(wrapper);
	}

	/**
	 * 更新用户-期望职位关联关系
	 */
	public static void resetRel(FdJobIntentions entity, List<Long> expectedPosition) {
		FdUserPositionRel.deleteByUserId(entity.getUserId());
		expectedPosition.forEach(e -> {
			FdJobSubcategory postion = FdJobSubcategory.findById(e);
			if(postion != null) {
				FdUserPositionRel rel = new FdUserPositionRel();
				rel.setUserId(entity.getUserId());
				rel.setPositionId(e);
				rel.setPositionName(postion.getName());
				rel.setIntentionId(entity.getId());
				rel.insert();
			}

		});
	}

	public static List<FdUserPositionRel> findAllByUserId(Long userId) {
		LambdaQueryWrapper<FdUserPositionRel> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(FdUserPositionRel::getUserId, userId);
		return new FdUserPositionRel().selectList(queryWrapper);

	}
}
